package com.spring.boot;

import com.spring.boot.bean.User;
import com.spring.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.ConfigurableApplicationContext;

/** 主程序类 @SpringBootApplication：这是一个SpringBoot应用 */

/**
 * @SpringBootApplication
 * 等同于
 * @SpringBootConfiguration
 * @EnableAutoConfiguration
 * @ComponentScan("com.spring.boot")
 */
@SpringBootApplication
public class Boot201HelloworldApplication {

  public static void main(String[] args) {
    // 1.返回我们IOC容器
    ConfigurableApplicationContext run =
        SpringApplication.run(Boot201HelloworldApplication.class, args);

    // 2.查看容器里面的组件
    for (String beanDefinitionName : run.getBeanDefinitionNames()) {
      System.out.println(beanDefinitionName);
    }
    // 3.从容器中获取组件
    User user01 = run.getBean("user01", User.class);
    User user02 = run.getBean("user01", User.class);

    System.out.println(user01 == user02); // true

    MyConfig bean = run.getBean(MyConfig.class);
    System.out.println(bean);

    //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
    //保持组件单实例
    User user = bean.user01();
    User user1 = bean.user01();
    System.out.println(user == user1); // true


    String[] beanNamesForType1 = run.getBeanNamesForType(WebMvcProperties.class);
    System.out.println("======"+beanNamesForType1.length);
  }


}
